package com.watson.spring.all.security.service.filter;

import com.watson.spring.all.security.service.security.TokenManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author watson
 * @date 2021/5/28 15:01
 * @Description: security授权过滤器
 **/
public class TokenAuthFilter extends BasicAuthenticationFilter {
    private TokenManager tokenManager;
    private RedisTemplate redisTemplate;

    public TokenAuthFilter(AuthenticationManager authenticationManager,
                           TokenManager tokenManager,
                           RedisTemplate redisTemplate) {
        super(authenticationManager);
        this.tokenManager = tokenManager;
        this.redisTemplate = redisTemplate;
    }


    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain chain) throws IOException, ServletException {
        //获取当前认证成功用户的权限信息
        UsernamePasswordAuthenticationToken authRequest = getAuthentication(request);
        //判断如果有权限信息，放到权限上下文中
        if (authRequest != null) {
            SecurityContextHolder.getContext().setAuthentication(authRequest);
        }
        chain.doFilter(request, response);

    }

    public UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request) {
        //从header中获取token
        String token = request.getHeader("token");
        if (token != null) {
            //token获取用户信息
            String username = tokenManager.getUserInfoForToken(token);
            List<String> list = (List<String>) redisTemplate.opsForValue().get(username);
            //list 转Collection
            Collection<GrantedAuthority> authorities = new ArrayList();
            list.forEach(item -> {
                SimpleGrantedAuthority auth = new SimpleGrantedAuthority(item);
                authorities.add(auth);
            });
            return new UsernamePasswordAuthenticationToken(username, token, authorities);
        }

        return null;


    }
}
